/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.softbook.view;

import br.com.softbook.controler.FornecedorControler;
import br.com.softbook.bean.Cadastrofornecedores;
import br.com.softbook.bean.Cidades;
import br.com.softbook.util.HibernateUtil;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.hibernate.Session;

/**
 *
 * @author Adriano
 */
public class formFornecedor extends javax.swing.JDialog {

    /**
     * Creates new form formFornecedor
     */
    private FornecedorControler fornecedorControler = new FornecedorControler();

    public formFornecedor(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        //Look And Feell
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        initComponents();
        this.setLocationRelativeTo(this);
        PreencherCombo();
        jTFCodigo.setEnabled(false);
        jBSalvar.setEnabled(false);
        jBDeletar.setEnabled(false);
        jBAlterar.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTFCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTFNomeFornecedor = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTFEnderecofornecedor = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTFNumero = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTFBairro = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTFCnpj = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTFEmail = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jTFTelefone = new javax.swing.JTextField();
        jTFCidade = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jBSalvar = new javax.swing.JButton();
        jBDeletar = new javax.swing.JButton();
        jBcancelar = new javax.swing.JButton();
        jBBuscar = new javax.swing.JButton();
        jBFechar = new javax.swing.JButton();
        jBNovo = new javax.swing.JButton();
        jBAlterar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Codigo");

        jLabel2.setText("Nome");

        jTFNomeFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTFNomeFornecedorActionPerformed(evt);
            }
        });
        jTFNomeFornecedor.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTFNomeFornecedorFocusLost(evt);
            }
        });

        jLabel3.setText("Endereço");

        jTFEnderecofornecedor.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTFEnderecofornecedorFocusLost(evt);
            }
        });

        jLabel4.setText("Numero");

        jTFNumero.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFNumeroKeyReleased(evt);
            }
        });

        jLabel5.setText("Bairro");

        jLabel6.setText("Cidade");

        jLabel7.setText("CNPJ/CPF");

        jTFCnpj.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTFCnpjFocusLost(evt);
            }
        });
        jTFCnpj.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFCnpjKeyReleased(evt);
            }
        });

        jLabel8.setText("Email");

        jLabel9.setText("Telefone");

        jTFTelefone.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTFTelefoneFocusLost(evt);
            }
        });
        jTFTelefone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTFTelefoneKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTFCodigo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jTFNomeFornecedor)
                        .addContainerGap())))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addGap(44, 44, 44))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jTFEnderecofornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTFNumero, javax.swing.GroupLayout.DEFAULT_SIZE, 67, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTFTelefone)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jTFCnpj, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .addComponent(jTFBairro)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING))
                        .addComponent(jLabel7)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTFEmail)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel6))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jTFCidade, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel9)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFNomeFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFEnderecofornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFCnpj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTFEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTFTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 12, Short.MAX_VALUE))
        );

        jBSalvar.setText("Salvar");
        jBSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBSalvarActionPerformed(evt);
            }
        });

        jBDeletar.setText("Deletar");
        jBDeletar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBDeletarActionPerformed(evt);
            }
        });

        jBcancelar.setText("Cancelar");
        jBcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBcancelarActionPerformed(evt);
            }
        });

        jBBuscar.setText("Buscar");
        jBBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBBuscarActionPerformed(evt);
            }
        });

        jBFechar.setText("Fechar");
        jBFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBFecharActionPerformed(evt);
            }
        });

        jBNovo.setText("Novo");
        jBNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNovoActionPerformed(evt);
            }
        });

        jBAlterar.setText("Alterar");
        jBAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jBNovo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBDeletar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jBAlterar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBFechar)
                        .addGap(10, 10, 10)))
                .addComponent(jBcancelar)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBcancelar)
                    .addComponent(jBDeletar)
                    .addComponent(jBBuscar)
                    .addComponent(jBNovo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBAlterar)
                    .addComponent(jBFechar)
                    .addComponent(jBSalvar))
                .addGap(0, 21, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(21, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(414, 390));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jBSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBSalvarActionPerformed
        if (jTFNomeFornecedor.getText().trim().equals("") || jTFNomeFornecedor.getText() == null) {
            JOptionPane.showMessageDialog(null, "Verificar o campo nome!");
            jTFNomeFornecedor.requestFocus();
        } else {
            Salvar();
            limpaCampos();
        }

    }//GEN-LAST:event_jBSalvarActionPerformed

    private void jBDeletarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBDeletarActionPerformed
        Deletar();
        limpaCampos();
    }//GEN-LAST:event_jBDeletarActionPerformed

    private void jBcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBcancelarActionPerformed
        limpaCampos();
    }//GEN-LAST:event_jBcancelarActionPerformed

    private void jBBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBBuscarActionPerformed
        Consultar();
        jBDeletar.setEnabled(true);
        jBSalvar.setEnabled(false);
        jBAlterar.setEnabled(true);

    }//GEN-LAST:event_jBBuscarActionPerformed

    private void jBFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBFecharActionPerformed
        Fechar();
    }//GEN-LAST:event_jBFecharActionPerformed

    private void jTFNomeFornecedorFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTFNomeFornecedorFocusLost
    }//GEN-LAST:event_jTFNomeFornecedorFocusLost

    private void jTFEnderecofornecedorFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTFEnderecofornecedorFocusLost
        if (jTFEnderecofornecedor.getText().trim().equals("") || jTFEnderecofornecedor.getText() == null) {
            JOptionPane.showMessageDialog(null, "Verificar o campo endereco!");
            jTFEnderecofornecedor.requestFocus();
        }
    }//GEN-LAST:event_jTFEnderecofornecedorFocusLost

    private void jTFCnpjFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTFCnpjFocusLost
        if (jTFCnpj.getText().trim().equals("") || jTFCnpj.getText() == null) {
            JOptionPane.showMessageDialog(null, "Verificar o campo CNPJ!");
            jTFCnpj.requestFocus();
        }
    }//GEN-LAST:event_jTFCnpjFocusLost

    private void jTFTelefoneFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTFTelefoneFocusLost
        if (jTFTelefone.getText().trim().equals("") || jTFTelefone.getText() == null) {
            JOptionPane.showMessageDialog(null, "Verificar o telefone nome!");
            jTFTelefone.requestFocus();
        }
    }//GEN-LAST:event_jTFTelefoneFocusLost

    private void jTFTelefoneKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFTelefoneKeyReleased
        jTFTelefone.setText(jTFTelefone.getText().replaceAll("[^0-9]", ""));
        jBSalvar.setEnabled(true);
    }//GEN-LAST:event_jTFTelefoneKeyReleased

    private void jTFNumeroKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFNumeroKeyReleased
        jTFNumero.setText(jTFNumero.getText().replaceAll("[^0-9]", ""));
    }//GEN-LAST:event_jTFNumeroKeyReleased

    private void jTFCnpjKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTFCnpjKeyReleased
        jTFCnpj.setText(jTFCnpj.getText().replaceAll("[^0-9]", ""));
    }//GEN-LAST:event_jTFCnpjKeyReleased

    private void jBNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNovoActionPerformed
        limpaCampos();
        criarFornecedor();
        jBSalvar.setEnabled(true);

    }//GEN-LAST:event_jBNovoActionPerformed

    private void jBAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBAlterarActionPerformed
        Cadastrofornecedores f = new Cadastrofornecedores();
        int id = Integer.parseInt(jTFCodigo.getText());
        f = fornecedorControler.buscar(id);
        f.setFornecedoresnome(jTFNomeFornecedor.getText());
        f.setFornecedoresendereco(jTFEnderecofornecedor.getText());
        f.setFornecedoresbairro(jTFBairro.getText());

        Cidades city = new Cidades();
        city.setIdcidades(jTFCidade.getSelectedIndex() + 1);
        f.setCidades(city);

        f.setFornecedoresnumero(Integer.parseInt(jTFNumero.getText()));
        f.setFornecedorescnpj(Integer.parseInt(jTFCnpj.getText()));
        f.setFornecedoresemail(jTFEmail.getText());
        f.setFornecedorestelefone(jTFTelefone.getText());
        fornecedorControler.salvar(f);
        JOptionPane.showMessageDialog(null, "Cadastro Alterado Com Sucesso", "Grupos", JOptionPane.INFORMATION_MESSAGE);
        limpaCampos();
    }//GEN-LAST:event_jBAlterarActionPerformed

    private void jTFNomeFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTFNomeFornecedorActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTFNomeFornecedorActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(formFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(formFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(formFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(formFornecedor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                formFornecedor dialog = new formFornecedor(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBAlterar;
    private javax.swing.JButton jBBuscar;
    private javax.swing.JButton jBDeletar;
    private javax.swing.JButton jBFechar;
    private javax.swing.JButton jBNovo;
    private javax.swing.JButton jBSalvar;
    private javax.swing.JButton jBcancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTFBairro;
    private javax.swing.JComboBox jTFCidade;
    private javax.swing.JTextField jTFCnpj;
    private javax.swing.JTextField jTFCodigo;
    private javax.swing.JTextField jTFEmail;
    private javax.swing.JTextField jTFEnderecofornecedor;
    private javax.swing.JTextField jTFNomeFornecedor;
    private javax.swing.JTextField jTFNumero;
    private javax.swing.JTextField jTFTelefone;
    // End of variables declaration//GEN-END:variables

    public Cadastrofornecedores getDadosTela() {
        Cadastrofornecedores fornecedor = new Cadastrofornecedores();
        fornecedor.setFornecedoresnome(jTFNomeFornecedor.getText());
        fornecedor.setFornecedoresendereco(jTFEnderecofornecedor.getText());
        fornecedor.setFornecedoresnumero(Integer.parseInt(jTFNumero.getText()));
        fornecedor.setFornecedoresbairro(jTFBairro.getText());

        Cidades city = new Cidades();
        city.setIdcidades(jTFCidade.getSelectedIndex() + 1);
        fornecedor.setCidades(city);

        fornecedor.setFornecedorescnpj(Integer.parseInt(jTFCnpj.getText()));
        fornecedor.setFornecedoresemail(jTFEmail.getText());
        fornecedor.setFornecedorestelefone(jTFTelefone.getText());
        return fornecedor;
    }

    public void setDadosTela(Cadastrofornecedores fornecedor) {
        jTFCodigo.setText(String.valueOf(fornecedor.getIdcadastrofornecedores()));
        jTFNomeFornecedor.setText(fornecedor.getFornecedoresnome());
        jTFTelefone.setText(String.valueOf(fornecedor.getFornecedorestelefone()));
        jTFEnderecofornecedor.setText(fornecedor.getFornecedoresendereco());
        jTFBairro.setText(fornecedor.getFornecedoresbairro());
        jTFEmail.setText(fornecedor.getFornecedoresemail());
        jTFNumero.setText(String.valueOf(fornecedor.getFornecedoresnumero()));

        Session session = HibernateUtil.getSession();
        jTFCidade.removeAllItems();
        List<Cidades> lista = session.createCriteria(Cidades.class).list();
        for (Cidades c : lista) {
            jTFCidade.addItem(c.getIdcidadesdescricao());
        }

        jTFCnpj.setText(String.valueOf(fornecedor.getFornecedorescnpj()));
        session.flush();
        session.close();
    }

    private void Salvar() {
        Cadastrofornecedores f = getDadosTela();
        fornecedorControler.salvar(f);
        JOptionPane.showMessageDialog(null, "Cadastro Salvo Com Sucesso", "Fornecedor", JOptionPane.INFORMATION_MESSAGE);
    }

    private void Deletar() {
        int id = Integer.parseInt(jTFCodigo.getText());
        Cadastrofornecedores f = new FornecedorControler().buscar(id);
        fornecedorControler.deletar(f);
        JOptionPane.showMessageDialog(null, "Cadastro excluido Com Sucesso", "Fornecedor", JOptionPane.INFORMATION_MESSAGE);
    }

    private void Fechar() {
        dispose();
    }

    private void Consultar() {
        formFornecedorConsulta c
                = new formFornecedorConsulta(null, true);
        c.setVisible(true);
        Cadastrofornecedores f = c.getFornecedor();
        if (f != null) {
            setDadosTela(f);
        } else {
            limpaCampos();
        }
    }

    public void limpaCampos() {
        jTFNomeFornecedor.setText("");
        jTFEnderecofornecedor.setText("");
        jTFEnderecofornecedor.setText("");
        jTFBairro.setText("");
        jTFEmail.setText("");
        jTFNumero.setText("");
        jTFCidade.setSelectedItem(null);
        jTFCnpj.setText("");
        jTFTelefone.setText("");
    }

    public void criarFornecedor() {
        fornecedorControler.criarFornecedor();
    }

    public void PreencherCombo() {
        Session session = HibernateUtil.getSession();
        jTFCidade.removeAllItems();
        List<Cidades> lista = session.createCriteria(Cidades.class
                ).list();
        for (Cidades c : lista) {
            jTFCidade.addItem(c.getIdcidadesdescricao());
        }
        session.flush();
        session.close();

    }
}
